﻿addLoadFuc(function() {
    initPageObjects();
});

function $id(ele) {
    return document.getElementById(ele);
}

//-------创建图形
var latestNodeId = 0;
var newPathId = 0;

function showContext(para) {
    alert(para.innerHTML);
}

var curSelectedAct;
//用来暂时存储活动

function mageMttrs(ele) {
    if (ele == null || ele.tagName != "WFACT") return;
    //当修改属性框属性时,修改活动的属性
    if (ele.getAttribute("wftype") == "actCommon") {
        $id("id_ipt_attr_name").value = ele.innerHTML;
    } else {
        $id("id_ipt_attr_name").value = ele.getAttribute("wfname");
    }
    $id("id_ipt_attr_id").value = ele.id;

    if (ele.getAttribute("wfin") == "N") {
        $id("id_sel_attr_in").selectedIndex = 1;
    } else {
        $id("id_sel_attr_in").selectedIndex = 0;
    }

    if (ele.getAttribute("wfout") == "N") {
        $id("id_sel_attr_out").selectedIndex = 1;
    } else {
        $id("id_sel_attr_out").selectedIndex = 0;
    }

    //----------列出路径----------
    var tb = $id("id_tb_attr_acts").children[0]; //获取tbody
    var count = tb.childNodes.length;
    for (var i = 1; i < count; i++) {
        tb.removeChild(tb.childNodes[1]);
    }
    var wfpath = document.getElementsByTagName("wfpath");
    count = wfpath.length;
    if (count > 0) {
        for (var i = 0; i < count; i++) {
            if (wfpath[i].getAttribute("start") == ele.id) {
                //------------找到路径后添加到右侧属性列表
                var newRow = tb.insertRow(1); // 插入一行
                var cel = newRow.insertCell(0);
                cel.innerHTML = wfpath[i].getAttribute("pathid");
                cel.style.display = "none";
                cel = newRow.insertCell(1);
                cel.innerHTML = ele.id;
                cel.style.display = "none";
                var endPoint = $id(wfpath[i].getAttribute("end"));
                newRow.insertCell(2).innerHTML = endPoint.getAttribute("wfname") == "" ? endPoint.innerHTML : endPoint.getAttribute("wfname");
                if (ele.getAttribute("wftype") == "routeOrSplit") {
                    var con = document.getElementById("wcf_" + wfpath[i].getAttribute("pathid"));
                    if (con == null || con == undefined) {
                        newRow.insertCell(3).innerHTML = "<input type='text' value='' onchange=\"setCondition(this)\" style='width:100px;'  />";
                    } else {
                        newRow.insertCell(3).innerHTML = "<input type='text' value='" + con.innerHTML + "' onchange=\"setCondition(this)\" style='width:100px;'  />";
                    }
                } else {
                    newRow.insertCell(3).innerHTML = "";
                }
                newRow.insertCell(4).innerHTML = "<label onclick=\"deletePath(this)\">x</label>";
            }
        }
    }
    $id("id_ipt_attr_module").value = (ele.getAttribute("wfmoddesc") == "" || ele.getAttribute("wfmoddesc") == null) ? "选择模块" : ele.getAttribute("wfmoddesc");
    //--------列出授权用户名
    var wfus = document.getElementsByTagName("wfuser");
    if (wfus && wfus.length > 0) {
        var ident = false;
        for (var j = 0; j < wfus.length; j++) {
            if (wfus[j].getAttribute("wfactid") == ele.id) {
                $id("lb_user_names").innerHTML = wfus[j].innerHTML.split("{")[1];
                ident = true;
                break;
            }
        }
        if (ident == false) {
            $id("lb_user_names").innerHTML = "";
        }
    } else {
        $id("lb_user_names").innerHTML = "";
    }
    curSelectedAct = ele;
}

function modifyName() {
    if (curSelectedAct == null || curSelectedAct.tagName != "WFACT") return;
    if (curSelectedAct.getAttribute("wftype") == "actCommon") {
        var text = $id("id_ipt_attr_name").value;
        if (text.length > 6) {
            curSelectedAct.innerHTML = text;
            curSelectedAct.setAttribute("class", curSelectedAct.className.replace("actname", "actnameSmall"));
        } else {
            curSelectedAct.innerHTML = text;
            curSelectedAct.setAttribute("class", curSelectedAct.className.replace("actnameSmall", "actname"));
        }
    } else {
        curSelectedAct.setAttribute("wfname", $id("id_ipt_attr_name").value);
    }
}

function modifyOut() {
    if (curSelectedAct == null || curSelectedAct.tagName != "WFACT") return;
    if ($id("id_sel_attr_out").selectedIndex == 1) {
        curSelectedAct.setAttribute("wfout", "N");
    } else {
        curSelectedAct.setAttribute("wfout", "Y");
    }
}

function modifyIn() {
    if (curSelectedAct == null || curSelectedAct.tagName != "WFACT") return;
    if ($id("id_sel_attr_in").selectedIndex == 1) {
        curSelectedAct.setAttribute("wfin", "N");
    } else {
        curSelectedAct.setAttribute("wfin", "Y");
    }
}

function div_float_close() {
    $id("div_float_container").style.display = "none";
}

//--------更改路径转移条件

function setCondition(ipt) {
    if (curSelectedAct == null || curSelectedAct.tagName != "WFACT") return;
    var pathid = ipt.parentNode.parentNode.firstChild.innerHTML;
    $id("wcf_" + pathid).innerHTML = ipt.value;
}

function btnOK(parameters) {
    if ($id("lb_ident").innerHTML == "PATH") {
        btnModifyPath();
    } else if ($id("lb_ident").innerHTML == "MODULE") {
        btnModuleConfirm();
    }
    $id("lb_ident").innerHTML = "";
}

//-------载入流程

function loadPorcess() {
    var htms = ns.GetProcessInfo().value;
    if (htms == "") {
        alert("没有数据 !");
        return;
    }
    resetVar();
    $id("id_div_placeholder").innerHTML = htms;
    initPageObjects();

    //-------获取当前活动id最大值
    var acts = document.getElementsByTagName("wfact");
    var tempMax = -1;
    var count = acts.length;
    for (var i = 0; i < count; i++) {
        var actid = parseInt(acts[i].id.substr(4, acts[i].id.length - 4));
        if (actid > tempMax) tempMax = actid;
        acts[i].onclick = function() {
            mageMttrs.call(acts[i], this);
        };
    }
    latestNodeId = tempMax + 1;

    //获取当前活动的最大ID
    //
    var paths = document.getElementsByTagName("wfpath");
    tempMax = -1;
    count = paths.length;
    for (var i = 0; i < count; i++) {
        var pathid = parseInt(paths[i].id.substr(9, paths[i].id.length - 9));
        if (pathid > tempMax) tempMax = pathid;
        paths[i].onclick = function() {
            mageMttrs.call(paths[i], this);
        };
    }
    newPathId = tempMax + 1;
}

function changeColor(ipt) {
    ipt.style.backgroundColor = "#fff";
}

function changeDefaultColor(ipt) {
    ipt.style.backgroundColor = "#f5f5f5";
}

//-------------授权用户----------------------------

function modifyUsers() {
    var tempType = curSelectedAct.getAttribute("wftype");
    if (tempType != "actCommon" && tempType != "actStart") return; //只有一半活动或开始活动可以设置用户

    if (curSelectedAct == null || curSelectedAct.tagName != "WFACT") return;
    window.open("Authors.aspx?curid=" + curSelectedAct.id, null, " height=420,width=700,  Left=300px,Top=20px, menubar=no,titlebar=no,scrollbar=no,toolbar=no, status=no,location=no");
}